package com.ruoyi.did.service.impl;


import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.did.domain.DidMetadata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.did.mapper.DidDocumentMapper;
import com.ruoyi.did.domain.DidDocument;
import com.ruoyi.did.service.IDidDocumentService;

@Service
public class DidDocumentServiceImpl implements IDidDocumentService {
    @Autowired
    private DidDocumentMapper didDocumentMapper;
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public DidDocument selectDidDocumentById(Long id) {
        DidDocument doc = didDocumentMapper.selectDidDocumentById(id);
        // 将 metadata JSON 转为对象
        if (doc != null && doc.getMetadata() != null) {
            try {
                DidMetadata meta = objectMapper.readValue(doc.getMetadata(), DidMetadata.class);
                // 可根据需要在 doc 里附加 meta 对象
                System.out.println("解析后的 metadata 对象: " + meta);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return doc;
    }

    @Override
    public DidDocument selectDidDocumentByDid(String did) {
        return didDocumentMapper.selectDidDocumentByDid(did);
    }

    @Override
    public List<DidDocument> selectDidDocumentList(DidDocument didDocument) {
        return didDocumentMapper.selectDidDocumentList(didDocument);
    }

    @Transactional
    @Override
    public int insertDidDocument(DidDocument didDocument, DidMetadata metadata) {
        try {
            // 将对象转 JSON 字符串存入 metadata 字段
            String metaJson = objectMapper.writeValueAsString(metadata);
            didDocument.setMetadata(metaJson);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return didDocumentMapper.insertDidDocument(didDocument);
    }
    @Transactional
    @Override
    public int updateDidDocument(DidDocument didDocument, DidMetadata metadata) {
        try {
            // 更新 metadata
            String metaJson = objectMapper.writeValueAsString(metadata);
            didDocument.setMetadata(metaJson);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return didDocumentMapper.updateDidDocument(didDocument);
    }

    @Override
    @Transactional
    public int deleteDidDocumentByIds(Long[] ids) {
        int rows = 0;
        for (Long id : ids) {
            rows += didDocumentMapper.deleteDidDocumentById(id);
        }
        return rows;
    }

    @Override
    @Transactional
    public int deleteDidDocumentById(Long id) {
        return didDocumentMapper.deleteDidDocumentById(id);
    }


    @Override
    @Transactional
    public boolean revokeDid(String did) {
        DidDocument doc = didDocumentMapper.selectDidDocumentByDid(did);
        if (doc == null) {
            return false;
        }
        doc.setStatus("1");
        didDocumentMapper.updateDidDocument(doc);
        return true;
    }
}